package com.app.modules.web.pagetag;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("serial")
public class PageTagText extends TagSupport {
	private String formId = "document.forms[0]";

	public void setFormId(String formId) {
		this.formId = "document.getElementById('" + formId + "')";
	}
	
	public int doStartTag() throws JspException {
		// 取出Form里的Page对象.
		Page page = (Page) pageContext.findAttribute("page");

		// 打印隐藏翻页字段及翻页JavaScript.
		printNavigation(page);
		printTurnPageJavaScript();

		return EVAL_BODY_INCLUDE;
	}

	// 打印导航条.
	private void printNavigation(Page page) {
		StringBuffer html_body = new StringBuffer();
		html_body.append("<span style=\"font-size: 14px;\">")
				.append("共").append(page.getTotalCount())
				.append("条记录，当前第").append(page.getPageNo()).append("/").append(page.getTotalPages()).append("页，每页")
				.append("<input style=\"width:18px;height:13px;font-size:12px;\" type=\"text\" id=\"pageSize\" name=\"page.pageSize\" value=\"")
				.append(page.getPageSize()).append("\"").append(" onkeydown=\"javascript:if(event.keyCode == 13)return resetPageNoAndSubmitForm();\"").append("/>条记录");
		html_body.append("&nbsp;[");
		
		if (!page.isHasPre()) {
			html_body.append("首页");
			html_body.append("&nbsp;");
			html_body.append("上一页");
		} else {
			html_body.append(getHref(1,"首页"));
			html_body.append("&nbsp;");
			html_body.append(getHref(page.getPrePage(),"上一页"));
		}
		
		html_body.append("&nbsp;");
		
		if (!page.isHasNext()) {
			html_body.append("下一页");
			html_body.append("&nbsp;");
			html_body.append("末页");
		} else {
			html_body.append(getHref(page.getNextPage(),"下一页"));
			html_body.append("&nbsp;");
			html_body.append(getHref(page.getTotalPages(),"末页"));
		}
		
		html_body.append("]&nbsp;");
		html_body.append("转到");
		html_body.append(getJumpControl(page));
		html_body.append("</span>");
		
		pageContextWrite(html_body.toString());
	}
	
	// 根据当前页获取链接字.
	private String getHref(long pageNo,String pageText) {
			StringBuffer buf = new StringBuffer();
			buf.append("<a href=\"#\"");
			buf.append(" onclick=\"jumpPage(").append(pageNo).append(");return false;\">");
			buf.append(pageText);
			buf.append("</a>");
			return buf.toString();
	}	

	// 打印隐藏翻页字段及翻页JavaScript.
	private void printTurnPageJavaScript() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\n<script type='text/javascript'>\n");
		
		buffer.append("function jumpPage(pageNo){\n")
			  .append("   var form = ").append(formId).append(";\n")
			  .append("   var pageSize = form[\"page.pageSize\"].value;\n")
			  .append("   var reg = /^([1-9][0-9]*)$/;\n\n")
			  .append("   if(!reg.test(pageSize)){\n")
			  .append("      alert(\"请输入正整数的每页记录条数 ！\");\n")
			  .append("      return false;\n")
			  .append("   }\n\n")
			  .append("   form[\"pageNo\"].value = pageNo;\n")
			  .append("   form.submit();\n")
			  .append("}\n\n");
		
		buffer.append("function resetPageNoAndSubmitForm(){\n")
		  .append("   var form = ").append(formId).append(";\n")
		  .append("   var pageSize = form[\"page.pageSize\"].value;\n")
		  .append("   var reg = /^([1-9][0-9]*)$/;\n\n")
		  .append("   if(!reg.test(pageSize)){\n")
		  .append("      alert(\"请输入正整数的每页记录条数 ！\");\n")
		  .append("      return false;\n")
		  .append("   }\n\n")
		  .append("   form[\"pageNo\"].value = 1;\n")
		  .append("   form.submit();")
		  .append("}\n");
		
		buffer.append("</script>\n");
		
		pageContextWrite(buffer.toString());
	}
	
	// 跳转选择框 .
	private String getJumpControl(Page page) {
		int turnPage = page.getPageNo();
		StringBuffer buf = new StringBuffer();
		buf.append("<select id=\"pageNo\" name=\"").append("page.pageNo\" onchange=\"javascript:jumpPage(this.value)\" style=\"height:18px;font-size:12px;\">");
		long totalPages = page.getTotalPages();
		for (int i = 1; i <= totalPages; i++) {
			buf.append("<option value='").append(i);
			// 如果翻页参数等于当前页则选择该选项.
			if (turnPage == i) {
				buf.append("' selected");
			}
			else buf.append("'");
			buf.append(">").append(i).append("</option>");
		}
		buf.append("</select>");
		return buf.toString();
	}
	
	// 打印.
	private void pageContextWrite(String html){
		try {
			Writer out = pageContext.getOut();
			out.write(html);
			out.flush();
		}
		catch (IOException e) {
			throw new RuntimeException("pageContext write.io exception");
		}
	}

}
